<template>
  <div>
    <div>
      <span>评标专家拜访反馈{{(index + 1)}}&nbsp;&nbsp;&nbsp;&nbsp;</span>
      <span style="color: #888" v-if="model.submitdate">{{ model.username }}{{ formatDate(model.submitdate) }}填写&nbsp;&nbsp;&nbsp;&nbsp;</span>
      <span><el-button type="text" icon="el-icon-remove" size="medium" @click="deleteList" v-if="model.state === 0 || model.state === 2 || model.state === null || model.state === undefined || model.state === ''">删除</el-button></span>
      <span><el-button type="text" icon="el-icon-circle-plus" size="medium" @click="openAddCommentDialog" v-if="model.state === 9">新增评论</el-button></span>
    </div>
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px">
      <el-row>
        <el-col :span="model.state === 9 ? 8 : 8">
          <el-form-item label="拜访时间" prop="visitdate" label-width="200px" :rules="rules.validateRequire">
            <el-date-picker v-model="model.visitdate" type="date" placeholder="拜访时间" value-format="yyyy-MM-dd" :disabled="disableEditSubmit" :picker-options="pickerOptions"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="model.state === 9 ? 5 : 8">
          <el-form-item label="专家岗位" prop="expertpost" label-width="100px" :rules="rules.validateRequire">
            <el-input v-model="model.expertpost"  :disabled="disableEditSubmit"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="model.state === 9 ? 6 : 8">
          <el-form-item label="我司拜访人员" prop="visitname" :rules="rules.validateRequire">
            <el-input v-model="model.visitname"  :disabled="disableEditSubmit"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="model.state === 9 ? 5 : 8" v-if="model.state === 9">
          <el-form-item label="是否有效" prop="canreal">
            <el-select v-model="model.canreal" filterable clearable :disabled="true">
              <el-option v-for="item in codeModel.enabledList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="专家意见" prop="expertadvise" label-width="200px" :rules="rules.validateRequire">
            <el-input v-model="model.expertadvise" type="textarea" :autosize="{ minRows: 1, maxRows: 3 }" maxlength="1000" show-word-limit :disabled="disableEditSubmit"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8" v-if="(fromApproval === '1' || model.canreal) && model.state !== 9">
          <el-form-item label="是否有效" prop="canreal" label-width="200px" :rules="fromApproval === '1' ? rules.validateRequire : []">
            <el-select v-model="model.canreal" filterable clearable :disabled="disableEditSubmit && !fromApproval">
              <el-option v-for="item in codeModel.enabledList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="fromApproval === '1'">
          <el-form-item label="建议内容" prop="advisecomment" label-width="200px" :rules="fromApproval === '1' && model.canreal === '0' ? rules.validateRequire : []">
            <el-input v-model="model.advisecomment" type="textarea" maxlength="1000" show-word-limit :autosize="{ minRows: 1, maxRows: 3 }"  :disabled="disableEditSubmit && !fromApproval"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <tl-comment-list ref="commentForm" :commentList="commentList"></tl-comment-list>
    </el-form>
    <tl-comment-edit ref="commentEdit" :expertInfo="model" @refresh="refresh"></tl-comment-edit>
  </div>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
import { validateNull } from '@tapui/utils/validate'
import { formatDate } from '@tapui/utils/date'
import tlCommentList from '@/views/mpm/mpmProjectVisitComment/editList'
import tlCommentEdit from '@/views/mpm/mpmProjectVisitComment/edit'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: { tlCommentList, tlCommentEdit },
  data() {
    return {
      moduleTitle: '评分细则专家拜访',
      urlPrefix: '/mpm/projectVisitExpert/',
      url: {
        // 额外的url
        queryVisitComment: '/mpm/projectVisitComment/list'
      },
      commentList: [],
      pickerOptions: {
        disabledDate: (time) => {
            return time.getTime() > new Date().getTime()
        }
      }
    }
  },
  props: {
    newValue: {
      type: Object,
      default: () => {}
    },
    fromApproval: {
      type: String,
      default: ''
    },
    index: {
      type: Number,
      default: 0
    }
  },
  watch: {
    newValue: {
      handler(value, oldvalue) {
        this.model = value
        this.commentList = []
        // 新增、编辑、查看权限确定
        if (!this.model.hasOwnProperty('id')) {
          this.isAdd = true
          this.disableEditSubmit = false
        } else if (this.model.state === 1 || this.model.state === 9) {
          this.isAdd = false
          this.disableEditSubmit = true
        } else {
          this.isAdd = false
          this.disableEditSubmit = false
        }
        this.loaded()
      },
      immediate: true,
      deep: true
    }
  },
  computed: {
    canDelete() {
      return validateNull(this.model.id)
    }
  },
  methods: {
    loaded() {
      // 页面加载完毕执行
      if (!this.isAdd && this.model.docno) {
        this.title = this.title + `（单据编号：${this.model.docno}）`
      }
      // 如果存在评标专家拜访反馈的id,需要查询该反馈下的评论
      if (!validateNull(this.model.id)) {
        const param = {
          qry_expertid: this.model.id,
          currentPage: 1,
          pageSize: 2000,
          sidx: 'createon',
          sord: 'asc'
        }
        getAction(this.url.queryVisitComment, param).then((res) => {
          if (res.success) {
            this.commentList = res.result.dataList
          }
        })
      } else {
        this.commentList = []
      }
    },
    deleteList() {
      this.$emit('deleteList')
    },
    formatDate(date) {
      return validateNull(date) ? '' : '于' + formatDate(new Date(date ? date.toString().replace(/\-/g, '/') : date), 'yyyy-MM-dd')
    },
    openAddCommentDialog() {
      this.$refs.commentEdit.isAdd = true
      this.$refs.commentEdit.visible = true
      this.$refs.commentEdit.disableEditSubmit = false
    },
    // 刷新专家反馈list
    refresh() {
      this.$emit('refresh')
    }
  }
}
</script>
